using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemyCurve : MonoBehaviour {

    private static Dictionary<Const.ENNEMY_TYPE, Transform> _dictionnaryEnemyType = new Dictionary<Const.ENNEMY_TYPE, Transform>();
    private static Transform parentEnemy;

    public Const.ENNEMY_TYPE typeEnnemyCurve = Const.ENNEMY_TYPE.OLD_MEN;

    public int nbEnemyPop = 1;
    public float durationCurve = 1.0f;

    private List<Transform> _enemys = new List<Transform>();
    private AnimationClip _curveAnimation;
    private int indexEnemy = 0;

    //Launch Wave
    public void Launch()
    {
        //Create all enemys
        for (int i = 0; i < nbEnemyPop; ++i)
        {
            _enemys.Add(CreateEnemyAtStart(typeEnnemyCurve));
        }

    }

    //Get Animation Curve
    public AnimationClip GetAnimation()
    {
        if (_curveAnimation != null)
            return _curveAnimation;

        int nbNode = this.transform.GetChildCount();
        Transform[] nodes = new Transform[nbNode];

        for (int i = 0; i < nbNode; ++i)
        {
            nodes[i] = this.transform.GetChild(i);
        }

        

        _curveAnimation = SplineCurve.getAnimationBSpline(nodes, 3, 100, durationCurve);
        return _curveAnimation;
    }



    //Sort Array by transform name
    Transform[] SortByName(Transform[] tab)
    {
        int nbNode = tab.Length;
        for (int i = 0; i < nbNode -1; ++i)
        {

            for (int j = i + 1; j < nbNode -3; ++j)
            {

                if (int.Parse(tab[i].name) > int.Parse(tab[j].name))
                {
                    /*
                    Transform temp = tab[i];
                    tab[i] = tab[j];
                    tab[j] = temp;
                     */
                }
            }
        }
        return tab;
    }


    //Create enemy
    private Transform CreateEnemyAtStart(Const.ENNEMY_TYPE type)
    {
        Transform enemy = null;


        if (!_dictionnaryEnemyType.ContainsKey(type))
        {
            if (type == Const.ENNEMY_TYPE.OLD_MEN)
            {
                GameObject baseEnemy = Resources.Load("Prefabs/OldMen") as GameObject;
                baseEnemy.active = false;
                _dictionnaryEnemyType.Add(type, baseEnemy.transform);
            }

        }

        enemy = Instantiate(_dictionnaryEnemyType[type]) as Transform;
        //enemy.gameObject.active = false;        
        enemy.transform.position = this.transform.GetChild(0).position;

        if (parentEnemy == null)
        {
            parentEnemy = new GameObject().transform;
            parentEnemy.position = Vector3.zero;
            parentEnemy.name = "Enemys";
        }
        enemy.parent = parentEnemy;

        enemy.GetComponent<Enemy>().createEnnemy(this, type, name);//cre = this;

        return enemy;
    }

    //Set type of curve by string name
    public void SetTypeByName(string name)
    {
        int nbEnum = System.Enum.GetValues(typeof(Const.ENNEMY_TYPE)).Length;
        for (int i = 0; i < nbEnum; ++i)
        {
            if (name == ((Const.ENNEMY_TYPE)i).ToString())
            {
                typeEnnemyCurve = (Const.ENNEMY_TYPE)i;
                return;
            }
        }
    }

}
